/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.QueryGroup;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.domain.bo.IsfdModelBo;
import com.comac.ins.isfd.domain.vo.IsfdCompositeModelVo;
import com.comac.ins.isfd.domain.vo.IsfdCompositeModelVo;
import com.comac.ins.isfd.domain.vo.ModelCountObjectVo;
import com.comac.ins.isfd.domain.vo.ModelCountResultObjectVo;
import com.comac.ins.isfd.service.IIsfdModelService;
import com.comac.ins.isfd.service.IsfdCompositeModelService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * 复合材料模型库管理
 *
 * @author hxloongs
 * @date 2024-7-1
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/composite/model")
public class IsfdCompositeModelController extends BaseController {

    @Autowired
    private IsfdCompositeModelService IIsfdModelService;

    /**
     * 模型库分页列表查询
     */
    @SaCheckPermission("isfd:model:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdCompositeModelVo> list(@Validated(QueryGroup.class) IsfdModelBo bo, PageQuery pageQuery) {
        return IIsfdModelService.queryPageList(bo, pageQuery);
    }

    /**
     * 模型库新增保存
     *
     * @param file 文件
     */
    @SaCheckPermission("isfd:model:save")
    @Log(title = "上传model + 详细信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> save(@RequestParam("file") MultipartFile file,
                               @RequestParam("modelName") String modelName,
                               @RequestParam("modelType") String modelType,
                               @NotEmpty(message = "适用机型不能为空") @RequestParam("applicableModels") String applicableModels,
                               @NotEmpty(message = "机型系列不能为空") @RequestParam("modelSeries") String modelSeries,
                               @NotEmpty(message = "载荷情况不能为空") @RequestParam("loadConditions") String loadConditions,
                               @NotEmpty(message = "模型的应用范围不能为空") @RequestParam("modelScope") String modelScope,
                               @NotEmpty(message = "业务场景不能为空") @RequestParam("businessScenarios") String businessScenarios,
                               @RequestParam("modelDescription") String modelDescription,
                               @NotEmpty(message = "模型数据的来源方式不能为空") @RequestParam("sourceMethods") String sourceMethods) {
        IsfdModelBo isfdModelBo = new IsfdModelBo();
        isfdModelBo.setModelName(modelName);
        isfdModelBo.setModelType(modelType);
        isfdModelBo.setApplicableModels(applicableModels);
        isfdModelBo.setModelSeries(modelSeries);
        isfdModelBo.setLoadConditions(loadConditions);
        isfdModelBo.setModelScope(modelScope);
        isfdModelBo.setBusinessScenarios(businessScenarios);
        isfdModelBo.setModelDescription(modelDescription);
        isfdModelBo.setSourceMethods(sourceMethods);
        return toAjax(IIsfdModelService.save(file, isfdModelBo));
    }

    /**
     * 模型库下载文件
     *
     * @param modelId 模型库id
     */
    @SaCheckPermission("isfd:model:download")
    @GetMapping("/download/{modelId}")
    public void download(@PathVariable String modelId, HttpServletResponse response) throws IOException {
        IIsfdModelService.download(modelId, response);
    }

    /**
     * 删除模型库对象
     *
     * @param modelId 模型库id数组
     */
    @SaCheckPermission("isfd:model:remove")
    @Log(title = "模型库删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{modelId}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String modelId) {
        return toAjax(IIsfdModelService.deleteWithValidByIds(modelId, true));
    }

}
